$(function () {
    var systemdictionary_tab = $('#systemdictionary_tab');
    var systemdictionary_edit = $("#systemdictionary_edit");
    var systemdictionary_dialog = $("#systemdictionary_dialog");
    var systemdictionary_item_tab = $('#systemdictionary_item_tab');
    var systemdictionary_item_edit = $("#systemdictionary_item_edit");
    var systemdictionary_item_dialog = $("#systemdictionary_item_dialog");
    systemdictionary_tab.datagrid({
        url:'/systemDictionary/list',
        fit:true,
        fitColumns:true,
        border:false,
        rownumbers:true,
        singleSelect:true,
        toolbar:'#schoollinkman_tab_tool',
        columns:[[
            {field:'id',title:'编号',align:'center',width:50},
            {field:'name',title:'目录',align:'center',width:100},
            {field:'intro',title:'介绍',align:'center',width:100}
        ]],
        onClickRow:function (index, row) {
            var itemurl = "/systemDictionary/getItem?parentId=";
            itemurl = itemurl + row.id;
            //重新加载systemdictionary_item_tab
            systemdictionary_item_tab.datagrid("load",itemurl);
        }
    });
    systemdictionary_item_tab.datagrid({
        fit:true,
        fitColumns:true,
        border:false,
        rownumbers:true,
        singleSelect:true,
        toolbar:'#systemdictionary_item_tab_tool',
        columns:[[
            {field:'id',title:'编号',align:'center',width:50},
            {field:'name',title:'内容',align:'center',width:100},
            {field:'intro',title:'明细',align:'center',width:100}
        ]]
    });

    var objMethod = {
        //刷新页面
        reloadList:function () {
            systemdictionary_tab.datagrid("reload");
            systemdictionary_item_tab.datagrid("reload");
        },
        //关闭窗口
        cancel:function () {
            systemdictionary_dialog.dialog("close");
            systemdictionary_item_dialog.dialog("close");
        },
        save:function () {
            var url = "/systemdictionary/save";
            //获取字典id
            var systemdictionaryId = $("#systemdictionary_id").val();
            if (systemdictionaryId){
                url = "/systemdictionary/update"
            }
            //提交表单
            systemdictionary_edit.form("submit",{
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },
        //打开编辑窗口
        addsystemdictionary:function () {
            //清空表中数据
            systemdictionary_edit.form("clear");
            systemdictionary_dialog.dialog("setTitle","新增");
            systemdictionary_dialog.dialog("open");
        },
        //字典离职
        deletesystemdictionary:function () {
            //获取选中的行
            var selectedRow = systemdictionary_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定删除",function (yes) {
               if (yes){
                   $.post("/systemdictionary/delete",{id:selectedRow.id},function (data) {
                      if(data.success){
                          $.messager.alert("提示","操作成功","info",function () {
                              //刷新页面
                              objMethod.reloadList();
                          });
                      } else {
                          $.messager.alert("提示",data.msg,"error");
                      }
                   });
               }
            });
        },
        //字典编辑
        editsystemdictionary:function () {
            //获取选中的行
            var selectedRow = systemdictionary_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //设置表单回显--清空表中数据
            systemdictionary_edit.form("clear");

            systemdictionary_edit.form("load",selectedRow);
            systemdictionary_dialog.dialog("setTitle","编辑");
            systemdictionary_dialog.dialog("open");
        },
        //保存明细
        saveitem:function () {
            var url = "/systemdictionary/saveitem";
            //获取字典id
            var systemdictionaryId = $("#systemdictionary_item_id").val();
            if (systemdictionaryId){
                url = "/systemdictionary/updateitem"
            }
            //提交表单
            systemdictionary_edit.form("submit",{
                url:url,
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("提示","操作成功","info",function () {
                            //关闭窗口刷新页面
                            objMethod.cancel();
                            objMethod.reloadList();
                        });
                    }else {
                        $.messager.alert("提示",data.msg,"error");
                    }
                }
            });
        },
        //打开明细编辑窗口
        addsystemdictionaryitem:function () {
            //清空表中数据
            systemdictionary_item_edit.form("clear");

            //设置目录
            var selectedRow = systemdictionary_tab.datagrid("getSelected");

            $("#item_combobox").combobox("setValue",selectedRow.id);

            systemdictionary_item_dialog.dialog("setTitle","新增");
            systemdictionary_item_dialog.dialog("open");
        },
        //字典明细删除
        deletesystemdictionaryitem:function () {
            //获取选中的行
            var selectedRow = systemdictionary_item_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            $.messager.confirm("提示","是否确定删除",function (yes) {
               if (yes){
                   $.post("/systemdictionary/deleteitem",{id:selectedRow.id},function (data) {
                      if(data.success){
                          $.messager.alert("提示","操作成功","info",function () {
                              //刷新页面
                              objMethod.reloadList();
                          });
                      } else {
                          $.messager.alert("提示",data.msg,"error");
                      }
                   });
               }
            });
        },
        //字典明细编辑
        editsystemdictionaryitem:function () {
            //获取选中的行
            var selectedRow = systemdictionary_item_tab.datagrid("getSelected");
            //判断是否选中行
            if(!selectedRow){
                $.messager.alert("提示","请选中一行数据","error");
                return;
            }
            //设置表单回显--清空表中数据
            systemdictionary_item_edit.form("clear");
            selectedRow['parentId'] = selectedRow.parentId;

            systemdictionary_item_edit.form("load",selectedRow);
            systemdictionary_item_dialog.dialog("setTitle","编辑");
            systemdictionary_item_dialog.dialog("open");
        }

    };
    //统一绑定事件
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        objMethod[cmd]();
    });
    //新增字典弹框
    systemdictionary_dialog.dialog({
        title: '新增',
        closed: true,
        buttons:'#systemdictionary_edit_tool'
    });
    //新增字典明细弹框
    systemdictionary_item_dialog.dialog({
        title: '新增',
        closed: true,
        buttons:'#systemdictionary_item_edit_tool'
    });
});
